import random

from flask import session
from flask_restful import Resource, reqparse, fields, marshal_with

from app.models import User, db, Collection, Poem, Author, Dynasty

parse = reqparse.RequestParser()
parse.add_argument('u_name', type=str, help='请输入手机号')
parse.add_argument('u_password', type=str, help='请输入密码')


poem_all = {
    "p_id": fields.Integer(attribute="p_id"),
    "p_name": fields.String(attribute="p_name"),
    'p_content': fields.String(attribute='p_content')
}

author_all = {
    "a_id": fields.Integer(attribute="a_id"),
    "a_name": fields.String(attribute="a_name"),
}

dynasty_all = {
    "d_id": fields.Integer(attribute="d_id"),
    "d_name": fields.String(attribute="d_name"),
}

home_info = {
    'status': fields.Integer,
    'poems': fields.Nested(poem_all),
    'authors': fields.Nested(author_all),
    'dynastys': fields.Nested(dynasty_all),
}


class HomeResource(Resource):
    @marshal_with(home_info)
    def get(self):
        num = 72326
        li = []
        while True:
            li.append(random.randint(0, num))
            s = set(li)
            if len(set(li)) == 15:
                break
        poems = [Poem.query.filter(Poem.p_id == i).first() for i in s]
        authors = [Author.query.filter(Author.a_id == i.p_author_id).first() for i in poems]
        dynastys = [Dynasty.query.filter(Dynasty.d_id == i.p_dynasty_id).first() for i in poems]
        return {'status': 200,
                'poems': poems,
                'authors': authors,
                'dynastys': dynastys,
                }

    def post(self):
        pass

    def put(self):
        pass

    def delete(self):
        pass
